<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
  <title>用户签到</title>
  <link rel="stylesheet" href="__PUBLIC__/Css/weui.css"/>
  <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
	<link rel="stylesheet" href="__PUBLIC__/Css/style.css"/>
  <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
	<script src=" https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 
  
</head>
<body ontouchstart="" > <!-- 触屏设置 -->

<div  id="com_home">
<!-- 导航条 -->
  <nav class="container-fluid navbar navbar-inverse ">
       <div class="navbar-header">
         <div class="navbar-brand" >
         	<span class="glyphicon glyphicon-arrow-left white"></span>
         	<span class="white">评论</span>
       </div></div>
  </nav>

<!-- 签到表单 -->
  <div class="col-lg-12 col-sm-12">    
      <div class="form-group">
          <textarea id="context" name="context" class="weui_textarea" placeholder="此刻想说什么.." rows="3"></textarea>
      </div>
      <div class="form-group">
          <div id="img"></div>
      </div>
      <div class="form-group text-center weui_uploader_input_wrp">
          <button id="selectImage" class="weui_uploader_input"></button>
      </div>
  </div>
<!--  发布签到 -->
  <div class="container padding col-lg-12 col-sm-12 col-xs-12" style="margin-top:2em;"> 
      <button class="weui_btn weui_btn_primary" id="submit"  >发布</button>
  </div>
</div>
  <!-- Msg -->
<div type="text/html" id="com_msg" style="display: none;">
  <div class="weui_msg">
      <div class="weui_icon_area"><i class="weui_icon_success weui_icon_msg"></i></div>
      <div class="weui_text_area">
          <h2 class="weui_msg_title">签到成功</h2>
          <p class="weui_msg_desc">积分增加2，回到优惠页</p>
      </div>
      <div class="weui_opr_area">
          <p class="weui_btn_area">
              <a class="weui_btn weui_btn_primary" id="confirm">好的</a>
          </p>
      </div>
  </div>
</div>
</body>
<!-- 微信页面配置脚本 -->
<script>
 wx.config({
    debug:true,
    appId: '{$signPackage.appId}',
    timestamp: '{$signPackage.timestamp}',
    nonceStr: '{$signPackage.nonceStr}',
    signature: '{$signPackage.signature}',
    jsApiList: [
      // 所有要调用的 API 列表
      'checkJsApi',
      'chooseImage',
      'uploadImage',
      'downloadImage',      
      'previewImage'
    ]
  }); 

</script>
<script type="text/javascript">
  /**
 * 用户签到-- 图片上传 / 发布签到
 * author:yangying
 */
var images = {
    localId: [],
    serverId: [],
    downloadId: []
 };  

 function uploadImage(){
        //用来存serverid的数组
        images.serverId = [];
        $(function(){
            $.each(images.localId, function(i,n) {
                wx.uploadImage({
                    localId: n,
                    success: function (res) {
                        //alert(JSON.stringify(res));
                        images.serverId.push(res.serverId);
                        if(i+1>=images.localId.length){
                          $.ajax({
                            url: "__CONTROLLER__/getcomment",
                            type: 'POST',
                            data: {
                                    'user': '2',
                                    'prefe': '1',
                                    'context': $('#context').val(),
                                    'image': images.serverId.join("&")
                                  },
                            success: function(data){
                                $('#com_home').hide();
                                $('#com_msg').show();
                            },
                            error: function(err, msg){
                                alert('err:'+msg);
                            }
                          }) // ajax end
                        }

                      

                    }/*,
                    fail: function (res) {
                        alert(JSON.stringify(res));
                    }*/
                }); // wx.uploadImage end
            }); // each end
        });
};  // fn uploadImage end


wx.ready(function () {
// 图片选择  
    $('#selectImage').click(function(){
        wx.chooseImage({
            success: function (res) {
                images.localId = res.localIds;
                $(function(){
                    $.each( res.localIds, function(i, n){
                        $("#img").append('<img src="'+n+'" style="width:80px;height:80px;margin-right:5px;margin-bottom:5px;" /> <br />');

                    });
                });
            }
        }); // wx.chooseImage end
    }); // select click end 

 $('#com_msg').hide();
/* 提交评论*/
    $('#submit').click(function() {    
         uploadImage(); // 调用图片上传方法
       
    }); // onclick end
    /*跳转使用优惠*/
    $('#confirm').click(function() {
        window.location.href="../Coupon/coupon-ok";
    });


     wx.error(function(res){
        var str = res.errMsg;
        var reg = /invalid signature$/;
        var r = str.match(reg);
        if(r !== null) {
            $(function(){
                $.getJSON('http://www.demo.com/tp/home/index/ticket', function(data) {
                    if(data) {
                        alert('ticket update');
                        location = location;
                        window.navigate(location);
                    }
                });
            });
        }
    });
}); //wx.ready end






</script>


</html>
